package com.shwread.android.ui.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;

import com.shwread.android.qysw10000227.R;
import com.tgx.tina.android.ipc.framework.BaseActivity;

/**
 * 阅读界面设置功能
 * @author Administrator
 *
 */
public class PopWindowSet
        extends
        ReadingPopWindow
        implements
        OnClickListener
{
	private static ReadingPopWindow instance;
//	ImageView       ivSetFullScreen;
	TextView        tvOneMinute, tvThreeMinute, tvFiveMinute, tvTenMinute, tvOftenLight;
	private Context context;
	
	public PopWindowSet(Context context, int time) {
		this.context = context;
		View contentView = LayoutInflater.from(context).inflate(R.layout.menu_pop_set, null);
		init(context, contentView);
		initLight(time);
	}
	
	public static ReadingPopWindow getInstance(Context context, int time) {
		if (instance == null) instance = new PopWindowSet(context, time);
		return instance;
	}
	
	public void initLight(int time) {
		time = getScreenOffTime() / 1000 / 60;
		if (time == 1)
		{
			tvOneMinute.setBackgroundResource(R.drawable.one_minute_pressed);
		}
		else if (time == 3)
		{
			tvThreeMinute.setBackgroundResource(R.drawable.three_minute_pressed);
		}
		else if (time == 5)
		{
			tvFiveMinute.setBackgroundResource(R.drawable.five_minute_pressed);
		}
		else if (time == 10)
		{
			tvTenMinute.setBackgroundResource(R.drawable.ten_minute_pressed);
		}
		else
		{
			tvThreeMinute.setBackgroundResource(R.drawable.three_minute_pressed);
			setScreenOffTime(3 * 60 * 1000);
		}
	}
	
	@Override
	protected void initView(View parentView) {
		tvOneMinute = (TextView) parentView.findViewById(R.id.menu_pop_one_minute);
		tvThreeMinute = (TextView) parentView.findViewById(R.id.menu_pop_line_three_minute);
		tvFiveMinute = (TextView) parentView.findViewById(R.id.menu_pop_line_five_minute);
		tvTenMinute = (TextView) parentView.findViewById(R.id.menu_pop_line_ten_minute);
		tvOftenLight = (TextView) parentView.findViewById(R.id.menu_pop_line_often_light);
//		ivSetFullScreen = (ImageView) parentView.findViewById(R.id.menu_pop_set_img);
		
	}
	
	@Override
	protected void addListener() {
		tvOftenLight.setOnClickListener(this);
		tvOneMinute.setOnClickListener(this);
		tvThreeMinute.setOnClickListener(this);
		tvFiveMinute.setOnClickListener(this);
		tvTenMinute.setOnClickListener(this);
//		ivSetFullScreen.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.menu_pop_one_minute:
				//一分钟
				tvOneMinute.setBackgroundResource(R.drawable.one_minute_pressed);
				tvFiveMinute.setBackgroundResource(R.drawable.five_minute_unpress);
				tvThreeMinute.setBackgroundResource(R.drawable.three_minute_unpress);
				tvTenMinute.setBackgroundResource(R.drawable.ten_minute_unpress);
				tvOftenLight.setBackgroundResource(R.drawable.often_light_unpress);
				setScreenOffTime(1 * 60 * 1000);
				break;
			case R.id.menu_pop_line_three_minute:
				//三分钟
				setScreenOffTime(3 * 60 * 1000);
				tvThreeMinute.setBackgroundResource(R.drawable.three_minute_pressed);
				tvOneMinute.setBackgroundResource(R.drawable.one_minute_unpress);
				tvFiveMinute.setBackgroundResource(R.drawable.five_minute_unpress);
				tvTenMinute.setBackgroundResource(R.drawable.ten_minute_unpress);
				tvOftenLight.setBackgroundResource(R.drawable.often_light_unpress);
				break;
			case R.id.menu_pop_line_five_minute:
				//五分钟
				setScreenOffTime(5 * 60 * 1000);
				tvFiveMinute.setBackgroundResource(R.drawable.five_minute_pressed);
				tvOneMinute.setBackgroundResource(R.drawable.one_minute_unpress);
				tvThreeMinute.setBackgroundResource(R.drawable.three_minute_unpress);
				tvTenMinute.setBackgroundResource(R.drawable.ten_minute_unpress);
				tvOftenLight.setBackgroundResource(R.drawable.often_light_unpress);
				break;
			case R.id.menu_pop_line_ten_minute:
				//十分钟
				setScreenOffTime(10 * 60 * 1000);
				tvTenMinute.setBackgroundResource(R.drawable.ten_minute_pressed);
				tvOneMinute.setBackgroundResource(R.drawable.one_minute_unpress);
				tvFiveMinute.setBackgroundResource(R.drawable.five_minute_unpress);
				tvThreeMinute.setBackgroundResource(R.drawable.three_minute_unpress);
				tvOftenLight.setBackgroundResource(R.drawable.often_light_unpress);
				break;
			case R.id.menu_pop_line_often_light:
				//常亮
				((BaseActivity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
				tvOftenLight.setBackgroundResource(R.drawable.often_light_pressed);
				tvOneMinute.setBackgroundResource(R.drawable.one_minute_unpress);
				tvFiveMinute.setBackgroundResource(R.drawable.five_minute_unpress);
				tvThreeMinute.setBackgroundResource(R.drawable.three_minute_unpress);
				tvTenMinute.setBackgroundResource(R.drawable.ten_minute_unpress);
				break;
//			case R.id.menu_pop_set_img:
//				//全屏
//				break;
		}
	}
	
	/**
	 * 获得锁屏时间 毫秒
	 */
	private int getScreenOffTime() {
		int screenOffTime = 0;
		try
		{
			screenOffTime = android.provider.Settings.System.getInt(context.getContentResolver(), android.provider.Settings.System.SCREEN_OFF_TIMEOUT);
		}
		catch (Exception localException)
		{
			
		}
		return screenOffTime;
	}
	
	/**
	 * 设置背光时间 毫秒
	 */
	private void setScreenOffTime(int paramInt) {
		try
		{
			android.provider.Settings.System.putInt(context.getContentResolver(), android.provider.Settings.System.SCREEN_OFF_TIMEOUT, paramInt);
		}
		catch (Exception localException)
		{
			localException.printStackTrace();
		}
	}
}
